//构造函数原型混合模式 --->Node
function Node(node, type,subtype,isMain,name,parent,db_id,hasChild) { 
	this.node = node; 
	this.type = type;
	this.subtype = subtype;
	this.isMain = isMain;
	this.name = name;
	this.parent = parent;
	this.db_id = db_id;
	this.hasChild = hasChild;
	//存放该结点的ports，名称为key,type为value
	this.ports = new Map();
	//存放该结点的properties，名称为key,type为value
	this.properties = new Map();
} 

Node.prototype.getType = function () { 
	return this.type;
};
Node.prototype.getSubType = function () { 
	return this.subtype;
};
Node.prototype.getIsMian = function () { 
	return this.isMain;
};
Node.prototype.getParent = function () { 
	return this.parent;
};
Node.prototype.getName = function () { 
	return this.name;
};

Node.prototype.getNode = function () { 
	return this.node;
};

Node.prototype.getDBId = function () { 
	return this.db_id;
};

Node.prototype.getPorts = function () { 
	return this.ports;
};

Node.prototype.getProperties = function () { 
	return this.properties;
};

Node.prototype.toXml = function () { 
	/****************start*************/
	var xml = "<node>";
	//add id
	xml += "<id>"+this.node.attributes.id+"</id>";
	//add type
	xml += "<type>"+this.type+"</type>";
	//add subType
	xml += "<subtype>"+this.subtype+"</subtype>";
	//add isMain
	xml += "<isMain>"+this.isMain+"</isMain>";
	//add name
	xml += "<name>"+this.name+"</name>";
	//add dbid
	if(this.db_id != null){
		xml += "<dbid>"+this.db_id+"</dbid>";
	}else{
		xml += "<dbid></dbid>";
	}
	//add parentid
	if(this.parent != null){
		xml += "<parentid>"+this.parent.getNode().attributes.id+"</parentid>";
	}else{
		xml += "<parentid></parentid>";
	}
	//add hasChild
	xml += "<haschild>"+this.hasChild+"</haschild>";
	//add x
	xml += "<x>"+this.node.attributes.position.x+"</x>";
	//add y
	xml += "<y>"+this.node.attributes.position.y+"</y>";
	//add width
	xml += "<height>"+this.node.attributes.size.height+"</height>";
	//add height
	xml += "<width>"+this.node.attributes.size.width+"</width>";
	
	var size = this.ports.size();
	if(size > 0 ){
		var keys = this.ports.keys();
		xml += "<features>";
		for (var i=0;i<=size-1;i++) 
		{   var key = keys[i];
			var value = this.ports.get(key);
			xml += "<port>";
			xml += "<name>"+key+"</name>";
			xml += "<type>"+value+"</type>";
			xml += "</port>";
		}
		xml += "</features>";
	}	
	size = this.properties.size();
	if(size > 0 ){
		var keys = this.properties.keys();
		xml += "<properties>";
		for (var i=0;i<=size-1;i++) 
		{   var key = keys[i];
			var value = this.properties.get(key);
			xml += "<property>";
			xml += "<name>"+key+"</name>";
			xml += "<value>"+value+"</value>";
			xml += "</property>";
		}
		xml += "</properties>";
	}
	/****************end*************/
	xml += "</node>";
	return xml;
};

Node.prototype.addPort = function (name,type) { 
	this.ports.put(name,type);
};

Node.prototype.setPorts = function (ports) { 
	this.ports = ports;
};

Node.prototype.setParent = function (parent) { 
	this.parent = parent;
};

Node.prototype.deletePort = function (name) { 
	this.ports.remove(name);
};

Node.prototype.addProperty = function (name,value) {
	if(!this.properties.containsKey(name)){
		this.properties.put(name,value);
	}
};

Node.prototype.setProperties = function (properties) { 
	this.properties = properties;
};

Node.prototype.deleteProperty = function (name) { 
	this.properties.remove(name);
};

//构造函数原型混合模式 -->
function Connect(type,sourceId,sourcePort,targetId,targetPort) { 
	this.sourceId = sourceId;  //连线对应的起始结点id
	this.sourcePort = sourcePort;  //连线对应的起始结点port
	this.targetId = targetId;  //连线对应的终止结点id
	this.targetPort = targetPort;  //连线对应的终止结点port
	this.type = type; 
} 

Connect.prototype.getType = function () { 
	return this.type;
};

Connect.prototype.toValue = function () { 
	if(this.sourceId>this.targetId){
		return this.sourceId +":" +this.sourcePort +"-"+this.targetId +":"+this.targetPort;
	}else{
		return this.targetId +":"+this.targetPort+"-"+this.sourceId +":" +this.sourcePort;
	}
};

Connect.prototype.toXml = function () { 
	/****************start*************/
	var xml = "<link>";
	//add sourceId
	xml += "<sourceid>"+this.sourceId+"</sourceid>";
	//add sourcePort
	xml += "<sourceport>"+this.sourcePort+"</sourceport>";
	//add targetId
	xml += "<targetid>"+this.targetId+"</targetid>";
	//add targetPort
	xml += "<targetport>"+this.targetPort+"</targetport>";
	//add type
	xml += "<type>"+this.type+"</type>";
	/****************end*************/
	xml += "</link>";
	return xml;
};

function showOperation(){
	$("#operation_ul").toggle("normal");
	$("#feature_ul").hide();
	$("#type_ul").hide();
	var src = ($("#operation_img").attr("src").match("img/app/horizontal_flow.png"))
    ? "img/app/vertical_flow.png" 
    : "img/app/horizontal_flow.png";
	$("#operation_img").attr("src",src);
}

function showFeature(){
	$("#operation_ul").hide();
	$("#feature_ul").toggle("normal");
	$("#type_ul").hide();
	var src = ($("#feature_img").attr("src").match("img/app/horizontal_flow.png"))
    ? "img/app/vertical_flow.png" 
    : "img/app/horizontal_flow.png";
	$("#feature_img").attr("src",src);
}

function showType(){
	$("#operation_ul").hide();
	$("#feature_ul").hide();
	$("#type_ul").toggle("normal");
	var src = ($("#type_img").attr("src").match("img/app/horizontal_flow.png"))
    ? "img/app/vertical_flow.png" 
    : "img/app/horizontal_flow.png";
	$("#type_img").attr("src",src);
}